<template>
  <div class="app-container">
    <div class="search-content">
      <div class="title">问题查询</div>
      <div class="search-area">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="ID">
            <el-input v-model="formInline.id" placeholder="输入ID" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmitByUser">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="问题" align="center" width="300">
        <template slot-scope="scope">
          <span style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">{{ scope.row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column label="A" align="center">
        <template slot-scope="scope">
          <span style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">{{ scope.row.A }}</span>
        </template>
      </el-table-column>
      <el-table-column label="B" align="center">
        <template slot-scope="scope">
          <span style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">{{ scope.row.B }}</span>
        </template>
      </el-table-column>
      <el-table-column label="C" align="center">
        <template slot-scope="scope">
          <span style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">{{ scope.row.C }}</span>
        </template>
      </el-table-column>
      <el-table-column label="D" align="center">
        <template slot-scope="scope">
          <span style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">{{ scope.row.D }}</span>
        </template>
      </el-table-column>
      <el-table-column label="答案" align="center" width="100">
        <template slot-scope="scope">
          <span style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">{{ scope.row.correct }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" width="300">
        <template slot-scope="scope">
          <span style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">{{ scope.row.describe }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分值" align="center" width="100">
        <template slot-scope="scope">
          <span style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">{{ scope.row.add_count }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="240">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ format(scope.row.createdAt) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import dayjs from 'dayjs'

export default {
  name: 'Integral',
  data() {
    return {
      formInline: {},
      listLoading: true,
      list: null,
      currentPage: 1,
      pageSize: 10,
      total: null
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async handleDelete(row) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete(`/cms/question/delete/${row.id}`).then(res => {
          if (res.code === 0) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getData()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleEdit(row) {
      this.$router.push(`/question/edit/${row.id}`)
    },
    async onSubmitByUser() {
      const res = await this.$http.get(`/cms/question/searchById?id=${this.formInline.id}`)
      if (res.code === 0) {
        this.list = res.data.rows
        this.total = res.data.count
      }
    },
    async getData() {
      const res = await this.$http.get(`/cms/question/list?offset=${this.currentPage}&limit=${this.pageSize}`)
      if (res.code === 0) {
        this.list = res.data.rows
        this.total = res.data.count
        this.listLoading = false
      }
    },
    format(date) {
      return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>
